package com.ruoyi.dc.service.impl;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.ExecUtil;
import com.ruoyi.dc.service.DynamicCorrectionService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.*;


@Service
public class DynamicCorrectionServiceImpl implements DynamicCorrectionService {

    @Value("${python.dc.getHeight.path}")
    private String heightPath;
    @Value("${python.dc.getHeight.name}")
    private String heightName;


    @Override
    public AjaxResult getDataByTime(String startTime, String endTime) {
        //拼装python执行命令
        String command = "python "+heightPath+heightName+" "+startTime.replaceAll(" ","-")+" "+endTime.replaceAll(" ","-");
        System.out.println(command);
        //调用Python
        String results = ExecUtil.execCmdWithResult(command);
        System.out.println(results);
        //进行Json字符串转换
        Map<String,Object> map;
        if (results.contains("###")){
            String dataResult = results.split("###")[1].replace("'", "\"");
            map = JSON.parseObject(dataResult, Map.class);
            if (map.isEmpty()){
                return AjaxResult.warn("所选时间范围内没有适用的卫星!");
            }
            //拼装返回内容
            Map<String,Object> resMap = new HashMap<>();
            Iterator<Map.Entry<String, Object>> it = map.entrySet().iterator();
            List<String> lableList = new ArrayList<>();
            List<String> valueList = new ArrayList<>();
            while(it.hasNext()) {
                Map.Entry<String, Object> entry = it.next();
                List<Object> value = (List<Object>) entry.getValue();
                lableList.add(value.get(0)+"—"+value.get(1)+" km ("+entry.getKey()+")");
                valueList.add(entry.getKey()+"###"+value.get(0)+","+value.get(1));
            }
            resMap.put("value",valueList);
            resMap.put("lable",lableList);
            resMap.put("content",results.split("###")[0]);
            return AjaxResult.success("获取成功！",resMap);
        }else {
            return AjaxResult.success("获取成功！");
        }
    }
}
